package com.orange.core.service.product;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.mapper.product.CreditProductMapper;
import com.orange.core.mapper.shop.ShopMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.product.CreditProduct;
import com.orange.core.pojo.entity.shop.Shop;
import com.orange.core.pojo.req.product.CreditProductPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class CreditProductService extends ServiceImpl<CreditProductMapper, CreditProduct> {

    private final ShopMapper shopMapper;

    public Page<CreditProduct> page(PageReqDto<CreditProductPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new CreditProductPageReq());
        }
        CreditProductPageReq condition = req.getCondition();
        Page<CreditProduct> page = this.lambdaQuery()
                .eq(condition.getCategoryId() != null, CreditProduct::getCategoryId, condition.getCategoryId())
                .like(StrUtil.isNotBlank(condition.getName()), CreditProduct::getName, condition.getName())
                .eq(condition.getShelf() != null, CreditProduct::getShelf, condition.getShelf())
                .ge(condition.getMinCredit() != null, CreditProduct::getCredit, condition.getMinCredit())
                .le(condition.getMaxCredit() != null, CreditProduct::getCredit, condition.getMaxCredit())
                .orderByDesc(CreditProduct::getId)
                .page(req.toPage());
        List<CreditProduct> creditProducts = page.getRecords();
        if (CollUtil.isNotEmpty(creditProducts)) {
            List<Integer> shopIds = creditProducts.stream().map(CreditProduct::getShopId).toList();
            Map<Integer, Shop> shopMap = shopMapper.selectBatchIds(shopIds)
                    .stream().collect(Collectors.toMap(Shop::getId, Function.identity()));
            for (CreditProduct creditProduct : creditProducts) {
                Shop shop = shopMap.get(creditProduct.getShopId());
                creditProduct.setShopName(shop.getName());
                creditProduct.setShopAddress(shop.getAddress());
            }
        }
        return page;
    }

    public CreditProduct info(int id) {
        CreditProduct creditProduct = this.getById(id);
        Shop shop = shopMapper.selectById(creditProduct.getShopId());
        creditProduct.setShopName(shop.getName());
        creditProduct.setShopAddress(shop.getAddress());
        return creditProduct;
    }

    public boolean shelf(int id, int shelf) {
        return this.lambdaUpdate()
                .set(CreditProduct::getShelf, shelf)
                .eq(CreditProduct::getId, id)
                .update();
    }

    public boolean changeStock(int id, int stock) {
        return this.lambdaUpdate()
                .set(CreditProduct::getStock, stock)
                .eq(CreditProduct::getId, id)
                .update();
    }

    public boolean deductStock(int id, int stock) {
        Assert.isTrue(stock > 0, "库存扣减数量应大于0");
        Assert.isTrue(baseMapper.deductStock(id, stock), "库存不足");

        // 检查库存数量
        CreditProduct creditProduct = this.lambdaQuery()
                .eq(CreditProduct::getId, id)
                .one();
        if (Objects.equals(creditProduct.getStock(), 0)) {
            shelf(id, 0);
        }
        return true;
    }

    public boolean addStock(int id, int stock) {
        Assert.isTrue(stock > 0, "库存增加数量应大于0");
        return this.lambdaUpdate()
                .setSql("stock = stock + " + stock)
                .eq(CreditProduct::getId, id)
                .update();
    }
}
